const App = getApp();

Page({

	/**
	 *    页面的初始数据
	 */
	data: {
		imageActive: 0,
		imageActives: 0,
		tipstext: 0,
		tips_status: 0,
		intergral_status: 0,
		gxcli: 0,
		goods_num: 1,
		goodsDetail: {},
		address: {},
		options: {},
		address_id: 0,
		goods_id: 0,
		goods_sku_id: 0,
		goodsidshow: 0,
		wapping: []
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		let _this = this
		_this.setData({
			options: options
		});
	},
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
		let _this = this;
		if (!_this.data.options.address_id) {
			wx.setStorage({
				key: 'goodsInfos',
				data: _this.data.options,
			})
			_this.getInfo(_this.data.options, '')
		} else {
			wx.getStorage({
				key: 'goodsInfos',
				success: function(res) {
					_this.getInfo(res.data, _this.data.options.address_id)
				}
			})
		}
	},
	getInfo(options, address_id) {

		let _this = this
		App._post_form('order/cart', {
			cart_ids: options.cart_ids,
			type: 'order',
			address_id: 0,
			is_invoice: 0,
			gift_wrapping_id: '',
			gift_card_id: '',
			gift_content: ''
		}, function(res) {

			if (!address_id) {
				_this.setData({
					address: res.data.address_default,
					address_id: res.data.address_default ? res.data.address_default.address_id : 0
				});
			}

			for (let i = 0; i < res.data.goods_list.length; i++) {
				res.data.goods_list[i].goods_sku.goods_attr = JSON.parse(res.data.goods_list[i].goods_sku.goods_attr)
				_this.data.wapping.push({
					goods_id: res.data.goods_list[i].goods_id,
					gift_wrapping_id: 0,
					gift_card_id: 0,
					gift_content: ''
				})
			}
			_this.setData({
				goodsDetail: res.data,
				cart_ids: options.cart_ids
			});
		})
		if (address_id) {
			App._get('address/detail', {
				address_id: address_id
			}, (result) => {
				_this.setData({
					address: result.data.detail,
					address_id: result.data.detail.address_id
				})
			})
		}
	},

	addresslist() {
		wx.navigateTo({
			url: '/pages/address/index?type=cartNow'
		});
	},

	bindycxi() {
		wx.navigateTo({
			url: '/pages/agreement/index'
		});
	},

	intergral() {

		this.setData({
			intergral_status: this.data.intergral_status == 0 ? 1 : 0
		})
	},
	tips(e) {
		this.setData({
			goodsidshow: e.currentTarget.dataset.goodsid != this.data.goodsidshow ? e.currentTarget.dataset.goodsid : 0,
			imageActive: this.data.wapping[e.currentTarget.dataset.gindex]['gift_wrapping_id'],
			imageActives: this.data.wapping[e.currentTarget.dataset.gindex]['gift_card_id'],
			tipstext:this.data.wapping[e.currentTarget.dataset.gindex]['gift_content']
		})
	},
	gxclis() {
		this.setData({
			gxcli: this.data.gxcli == 0 ? 1 : 0
		})
	},
	imageChange(e) {
		let wappings = this.data.wapping;
		wappings[e.currentTarget.dataset.gindex]['gift_wrapping_id'] = e.currentTarget.dataset.index
		this.setData({
			imageActive: e.currentTarget.dataset.index,
			wapping: wappings
		})
	},
	imageChanges(e) {
		let wappings = this.data.wapping;
		wappings[e.currentTarget.dataset.gindex]['gift_card_id'] = e.currentTarget.dataset.index
		this.setData({
			imageActives: e.currentTarget.dataset.index,
			wappings: wappings
		})
	},
	bindTips(e) {
		let wappings = this.data.wapping;
		wappings[e.currentTarget.dataset.gindex]['gift_content'] = e.detail.value
		this.setData({
			tipstext: e.detail.value,
			wappings: wappings
		});
	},
	payOrder() {
		let re = this.data;
		if (re.gxcli == 0) {
			App.showError('请勾选YC商城用户隐私条款');
			return false;
		}
		console.log(this.data.wapping)
		let info = {
			"cart_ids":this.data.cart_ids,
			'type':'',
			'address_id':this.data.address_id,
			'is_invoice': 0,
			'images':  JSON.stringify(this.data.wapping)
		}
		App._post_form('order/cart', info, function(result) {
			if (result.code == 1) {
				let datas = result.data.payment
				let payments = {
					timeStamp: datas.timeStamp,
					paySign: datas.paySign,
					prepay_id: datas.prepayid,
					nonceStr: datas.nonceStr,
					signType: datas.signType
				}
				wx.requestPayment({
					'timeStamp': result.data.payment.timeStamp,
					'nonceStr': result.data.payment.nonceStr,
					'package': result.data.payment.package,
					'signType': result.data.payment.signType,
					'paySign': result.data.payment.paySign,
					'success': function(res) {
						if (res.errMsg == "requestPayment:ok") {
							wx.navigateTo({
								url: '/pages/placeorder/paysuc?order_id=' + result.data.order_id
							});
						}
					},
					'fail': function(res) {
						if (res.errMsg == "requestPayment:fail") {
							wx.navigateTo({
								url: '/pages/placeorder/payfail'
							});
						}
					},
					'complete': function(res) {

					}
				})
			}
		});
	}
});
